/*
 *	OpenSource code or fragments by agile webentwicklung, Marcus Fuhrmeister
 *
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions
 *	are met:
 *  
 *		* Redistribution of source code must retain the above OpenSource comment,
 *		this list of conditions and the following disclaimer.
 * 
 *		* Redistribution in binary form must reproduce the above OpenSource comment,
 *		this list of conditions and the following disclaimer in the
 *		documentation and/or other materials provided with the distribution.
 * 
 *	Neither the name of 'avocaod systems, Marcus Fuhrmeister' or the
 *	names of contributors may be used to endorse or promote products derived
 *	from this software without specific prior written permission.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 */
package de.fuhrmeister.browserchooser.view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

/**
 * Main view class.
 * 
 *	@date 20.11.2008
 *	@author		Marcus Fuhrmeister
 */
public class Docklet extends JFrame implements View {

	private static final long serialVersionUID = 5362898562666290511L;

	/**
	 * Constructor to create a new view.
	 */
	public Docklet() {
		super();
		this.setUndecorated(true);
	}
	
	

	/* (non-Javadoc)
	 * @see java.awt.Component#setBackground(java.awt.Color)
	 */
	@Override
	public void setBackground(Color c) {
		super.getContentPane().setBackground(c);
	}



	/**
	 * Sets the flow layout.
	 */
	public void setFlowLayout() {
		((JPanel) this.getContentPane()).setLayout(new FlowLayout());
	}

	/**
	 * Sets the line border.
	 * 
	 * @param color the color of the border
	 */
	public void setLineBorder(final Color color) {
		setBorder(new LineBorder(color));
	}

	/**
	 * Sets the border.
	 * 
	 * @param border the border
	 */
	private void setBorder(final LineBorder border) {
		((JPanel) this.getContentPane()).setBorder(border);
	}

	/**
	 * Returns the browser count.
	 * 
	 * @return the browser count
	 */
	public int getBrowserShortcutCount() {
		return this.getContentPane().getComponentCount();
	}

	/**
	 * Adds the given <code>BrowserShortcutView</code> to this view.
	 * 
	 * @param bsv the BrowserShortcutView to add
	 */
	public void addBrowserShortcut(final BrowserShortcutView bsv) {
		this.add(bsv);
		this.validateTree();
	}

	/**
	 * Adds the <code>BrowserShortcutView</code>s in the given list to this view.
	 * 
	 * @param bsv the list of BrowserShortcutViews
	 */
	public void addBrowserShortcuts(final ArrayList<BrowserShortcutView> bsv) {
		final Iterator<BrowserShortcutView> iterator = bsv.iterator();
		while (iterator.hasNext()) {
			this.add(iterator.next());
		}
		this.validateTree();
	}

	/**
	 * Removes the BrowserShortcutView with the given id from this view.
	 * 
	 * @param id the id of the browser to delete
	 */
	public void removeBrowserShortcut(final int id) {
		final BrowserShortcutView[] a = (BrowserShortcutView[]) this.getContentPane().getComponents();
		for (final BrowserShortcutView bsv : a) {
			if (bsv.getID() == id) {
				this.remove(bsv);
			}
		}
		this.validateTree();
	}

	/**
	 * Removes all browser views from this view.
	 */
	public void removeBrowserShortcuts() {
		this.getContentPane().removeAll();
		this.validateTree();
	}

	/**
	 * Registers the listeners.
	 */
	public void registerListener(final EventListener l) {
		if (l instanceof MouseListener) {
			this.getContentPane().addMouseListener((MouseListener) l);
		}
		if (l instanceof FocusListener) {
			this.getContentPane().addFocusListener((FocusListener) l);
		}
	}

	/**
	 * Returns a list of <code>BrowserShortcutView</code>s in this view.
	 * 
	 * @return a list of <code>BrowserShortcutView</code>s in this view
	 */
	public ArrayList<BrowserShortcutView> getBrowserShortcuts() {
		final BrowserShortcutView[] a = (BrowserShortcutView[]) this.getComponents();
		final ArrayList<BrowserShortcutView> aList = new ArrayList<BrowserShortcutView>(a.length);
		for (final BrowserShortcutView bsv : a) {
			aList.add(bsv);
		}
		return aList;

	}
}
